<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="操作描述" prop="job_desc">
            <el-input v-model="form.job_desc" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="操作命名" prop="job_namespace">
            <el-input v-model="form.job_namespace" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="ip" prop="jenkinsid">
            <el-select
              v-model="form.jenkinsid"
              remote
              reserve-keyword
              :loading="loading"
              @change="ipchange"
            >
              <el-option
                v-for="item in options"
                :key="item.jenkinsid"
                :label="item.ip"
                :value="item.jenkinsid"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="操作类型" prop="operate_type">
            <el-select
              v-model="form.operate_type"
              filterable
              placeholder="请选择"
              :loading="loading"
              @change="operatetypechange"
            >
              <el-option
                v-for="item in dict.operation"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="operateparamsshow" label="操作参数">
            <el-select
              v-model="form.operateparams"
              filterable
              placeholder="请选择"
              :loading="loading"
              :disabled="operateparamsdisabled"
              multiple=""
              width="200"
            >
              <el-option
                v-for="item in serverpaths"
                :key="item.serverpathid"
                :label="item.serverpath+' ('+item.pathdesc+')'"
                :value="item.serverpathid"
              />
            </el-select>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="jenkinsid" label="ip" />
        <el-table-column prop="job_desc" label="操作描述" />
        <el-table-column prop="job_namespace" label="操作命名" />
        <el-table-column prop="operate_type" label="操作类型">
          <template slot-scope="scope">
            {{ dict.label.operation[scope.row.operate_type] }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="operateparamsType" label="参数类型" /> -->
        <el-table-column prop="moduser" label="moduser" />
        <el-table-column prop="modtime" label="modtime" />
        <el-table-column v-if="checkPer(['admin','devopsOperation:edit','devopsOperation:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDevopsOperation from '@/api/devops/devopsJenkinsOperation'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getTrees } from '@/api/devops/devopsJenkinsClient'
import { getServerPath } from '@/api/devops/devopsServerPath'

const defaultForm = { operationid: null, job_desc: null, job_namespace: null, jenkinsid: null, operate_type: null, operateparams: null, flag: null, notes: null, reguser: null, regtime: null, moduser: null, modtime: null }
export default {
  name: 'DevopsOperation',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['operation'],
  cruds() {
    return CRUD({ title: 'DEVOPS:jenkins操作', url: 'api/devops/devopsOperation', idField: 'operationid', sort: 'operationid,desc', crudMethod: { ...crudDevopsOperation }})
  },
  data() {
    return {
      operateparamsdisabled: true,
      options: [],
      serverpaths: [],
      loading: false,
      operateparamsshow: false,
      permission: {
        add: ['admin', 'devopsOperation:add'],
        edit: ['admin', 'devopsOperation:edit'],
        del: ['admin', 'devopsOperation:del']
      },
      rules: {
        job_desc: [
          { required: true, message: '操作描述不能为空', trigger: 'blur' }
        ],
        job_namespace: [
          { required: true, message: '操作命名不能为空', trigger: 'blur' }
        ],
        jenkinsid: [
          { required: true, message: 'ip不能为空', trigger: 'blur' }
        ],
        operate_type: [
          { required: true, message: '操作类型不能为空', trigger: 'blur' }
        ]
      }}
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      this.loading = false
      getTrees().then((res) => {
        this.loading = false
        this.options = res
      }).catch(() => {
        this.loading = false
      })
      form.operateparams = JSON.parse(form.operateparams)
      this.operatetypechange()
      this.ipchange()
    },
    // 切换操作类型
    operatetypechange() {
      if (this.form.operate_type == 'mysql_exec' || this.form.operate_type == 'redis_add' || this.form.operate_type == 'redis_del') {
        // 如果是这些操作 操作参数框隐藏
        this.operateparamsshow = false
        // 清除值
        this.form.operateparams = null
      } else {
        this.operateparamsshow = true
      }
    },
    ipchange() {
      if (this.form.jenkinsid) {
        // 有ip 获取服务器路径
        // 通过jenkinsid 获取服务器路径
        getServerPath(this.form.jenkinsid).then((res) => {
          this.loading = false
          this.serverpaths = res.content
          this.operateparamsdisabled = false
        }).catch(() => {
          this.$message.error('获取服务器路径失败!')
          this.loading = false
        })
      } else {
        this.operateparamsdisabled = true
      }
    }
  }
}
</script>

<style scoped>

</style>
